﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class LOAI_BAI_VIETDAL:KetNoiDuLieu
    {
        public void Insert(LOAI_BAI_VIET loaibaiviet)
        {
            SqlCommand cm = new SqlCommand("sp_loaibv_Insert", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TEN_LOAI_BV", loaibaiviet.Tenloai);
            cm.Parameters.AddWithValue("@MA_LOAI_CHA", loaibaiviet.Maloaicha);
            cm.Parameters.AddWithValue("@TRANG_THAI", loaibaiviet.Trangthai);
            cm.ExecuteNonQuery();
        }

        public void Update(LOAI_BAI_VIET loaibaiviet)
        {
            SqlCommand cm = new SqlCommand("sp_loaibv_Update", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_LOAI_BV", loaibaiviet.Maloai);
            cm.Parameters.AddWithValue("@TEN_LOAI_BV", loaibaiviet.Tenloai);
            cm.Parameters.AddWithValue("@MA_LOAI_CHA", loaibaiviet.Maloaicha);
            cm.Parameters.AddWithValue("@TRANG_THAI", loaibaiviet.Trangthai);
            cm.ExecuteNonQuery();
        }

        public void Update_status(LOAI_BAI_VIET loaibaiviet)
        {
            SqlCommand cm = new SqlCommand("Update LOAI_BAI_VIET SET TRANG_THAI = " + loaibaiviet.Trangthai.ToString() + " WHERE MA_LOAI = " + loaibaiviet.Maloai, LayKetNoi());
            cm.ExecuteNonQuery();
        }

        public void Delete(int maloai)
        {
            SqlCommand cm = new SqlCommand("sp_loaibv_Delete", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_LOAI_BV", maloai);
            cm.ExecuteNonQuery();
        }

        public Loaibaivietcolection Get(string Where, int PageIndex, int PageSize, string OrderBy, string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_loaibv_GET", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Loaibaivietcolection loaibaivietcollection = new Loaibaivietcolection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                LOAI_BAI_VIET loaibv = new LOAI_BAI_VIET();
                loaibv.Maloai = int.Parse(ds.Tables[0].Rows[i]["MA_LOAI_BV"].ToString());
                loaibv.Tenloai = ds.Tables[0].Rows[i]["TEN_LOAI_BV"].ToString();
                loaibv.Maloaicha = int.Parse(ds.Tables[0].Rows[i]["MA_LOAI_CHA"].ToString());
                loaibv.Trangthai = int.Parse(ds.Tables[0].Rows[i]["TRANG_THAI"].ToString());
                loaibaivietcollection.Add(loaibv);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return loaibaivietcollection;
        }
    }
}
